<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('mobile');
            $grid->column('amount');
            $grid->column('created_at')->display(function ($time) {
                return date('Y-m-d H:i:s', strtotime($time));
            })->sortable();
//        $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('name');
                $filter->equal('mobile');
            });
            $grid->disableRowSelector();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
            $grid->model()->orderBy('id', 'desc');
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a href="/admin/user/coin/clues?user_id=' . $actions->getKey() . '"><i title="线索日志" class="feather icon-activity grid-action-icon"></i>&nbsp;&nbsp;</a>');
                $actions->append('<a href="/admin/user/amount/log?user_id=' . $actions->getKey() . '"><i title="金橡果流水" class="feather icon-stop-circle grid-action-icon"></i></a>');
            });

            // 添加导出功能，并格式化日期字段
            $grid->export()->filename('用户数据导出');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('mobile');
            $show->field('amount');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->tel('mobile');
            $form->number('amount');
        });
    }
}
